/**
 * 个人出差记录action类
 * @author 郑翎
 * 2015-10-1 11:07:43
 */
package com.cbts.action.person;

import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import com.cbts.action.travel.ApplayReimAction;
import com.cbts.entity.ApplayReim;
import com.cbts.entity.BusinessTravel;
import com.cbts.entity.BusinessTravelList;
import com.cbts.entity.Receipt;
import com.cbts.entity.UserInfo;
import com.cbts.service.info.UserInfoService;
import com.cbts.service.person.PersonTravelInfoService;
import com.cbts.service.travel.ApplayReimService;
import com.opensymphony.xwork2.ActionSupport;

public class PersonTravelInfoAction extends ActionSupport {
	
	private ApplayReimAction applayReimAction;				 	// 申请报销的跳转类
	private ApplayReimService applayReimService; 			 	// 申请报销的业务逻辑接口
	private PersonTravelInfoService personTravelInfoService; 	// 个人出差记录管理的业务逻辑接口
	private UserInfoService userInfoService;					// 用户信息的业务逻辑接口

	private List<BusinessTravelList> businessTravelLists;		// 出差记录列表
	private List<BusinessTravel> businessTravels;				// 出差信息列表
	private List<Receipt> receipts;								// 收据列表
	
	// 出差列表的6种状态分别对应的列表
	private List<BusinessTravelList> needChecks;				// 待审核的出差列表
	private List<BusinessTravelList> checkFails; 				// 审核不通过的出差列表
	private List<BusinessTravelList> checkSuccess; 				// 审核通过的出差列表
	private List<BusinessTravelList> needApplays;				// 待报销的出差列表
	private List<BusinessTravelList> applayFails;				// 报销不通过的出差列表
	private List<BusinessTravelList> applaySuccess; 			// 已报销的出差列表
	
	private ApplayReim applayReim;								// 报销记录
	private UserInfo userInfo;									// 用户信息
	private BusinessTravelList businessTravelList;				// 出差记录
	private String travelorder;									// 出差单号
	private String userid;										// 获取session中的userid
	private String travelstandrad;								//出差标准
	
	/**
	 *获得用户个人出差记录和报销记录 
	 * @return getsuccess 跳转到个人出差记录页面
	 * @return geterror 获取异常，跳转到异常处理首页
	 */
	public String getPersonTravelList() {
		
		try {
			
			String[] status = { "待审核", "审核不通过","审核通过", "待报销", "报销不通过", "已报销" };
			// Map<"状态",“是该状态的出差列表”>
			Map<String, List<BusinessTravelList>> personTravelMaps = personTravelInfoService
					.getBusinessTravelListMap(this.getUserid());
			
			userInfo = userInfoService.getUserInfo(this.getUserid());
			
			if (userInfo!=null&&userInfo.getUserGroup()!=null
					&&userInfo.getUserGroup().getTravelStandard()!=null) {
				travelstandrad = "您所在的"+userInfo.getUserGroup().getUserGroupName()
						+"的出差标准如下:\n"+"交通:"+userInfo.getUserGroup().getTravelStandard().getTraffic().getTraffic()
						+","+userInfo.getUserGroup().getTravelStandard().getTraffic().getStandard()
						+"  "+userInfo.getUserGroup().getTravelStandard().getTraffic().getPrice()+"元/公里\n"
						+"住宿:"+userInfo.getUserGroup().getTravelStandard().getHotel().getHotel()
						+"  "+userInfo.getUserGroup().getTravelStandard().getHotel().getPrice()+"元/天\n"
						+"餐饮费:"+userInfo.getUserGroup().getTravelStandard().getMealsCost()+"元/天\n"
						+"其它费用:"+userInfo.getUserGroup().getTravelStandard().getElseCost()+"元/天";
			}else {
				travelstandrad = "您不属于任何用户组，或者\n您所在的用户组不具备出差条件";
			}

			needChecks = personTravelMaps.get(status[0]);
			checkFails = personTravelMaps.get(status[1]);
			checkSuccess = personTravelMaps.get(status[2]);
			needApplays = personTravelMaps.get(status[3]);
			applayFails = personTravelMaps.get(status[4]);
			applaySuccess = personTravelMaps.get(status[5]);
			
			return "getsuccess";
		} catch (Exception e) {
			e.printStackTrace();
			return "geterror";
		}
	}

	/**
	 *根据出差单号获取具体的出差记录、出差信息列表、报销记录和收据 
	 * @return tisuccess 跳转到个人报销记录详情页面
	 * @return tierror 获取异常，跳转到异常处理首页
	 */
	public String getApplayInfoList() {
		
		try {
			
			Map session=ServletActionContext.getContext().getSession();
			
			if (session.get("userid")==null) {
				
				String[] temp = travelorder.split("-");
				session.put("userid", temp[temp.length-1]);
				this.setUserid(temp[temp.length-1]);
				
			}
			
			// 根据出差单号获取出差列表
			businessTravelList = applayReimService
					.getTravelListByOrder(travelorder);
			// 根据出差单号获取出差信息列表
			businessTravels = applayReimService.getTravelInfoList(travelorder);
			// 根据出差单号获得报销信息
			applayReim = applayReimService.getApplayReim(travelorder);
			// 根据出差单号获得收据列表
			receipts = applayReimService.getReceiptFile(travelorder);
			
			
			if (!receipts.isEmpty()&&receipts.size()!=0) {
				if (session.get("userid")==null) {
					applayReimAction.dowLoadList(receipts,userid);
				}else {
					applayReimAction.dowLoadList(receipts,this.getUserid());
				}
			}
			
			userInfo = userInfoService.getUserInfo(this.getUserid());
			
			if (userInfo!=null&&userInfo.getUserGroup()!=null
					&&userInfo.getUserGroup().getTravelStandard()!=null) {
				travelstandrad = "您所在的"+userInfo.getUserGroup().getUserGroupName()
						+"的出差标准如下:\n"+"交通:"+userInfo.getUserGroup().getTravelStandard().getTraffic().getTraffic()
						+","+userInfo.getUserGroup().getTravelStandard().getTraffic().getStandard()
						+"  "+userInfo.getUserGroup().getTravelStandard().getTraffic().getPrice()+"元/公里\n"
						+"住宿:"+userInfo.getUserGroup().getTravelStandard().getHotel().getHotel()
						+"  "+userInfo.getUserGroup().getTravelStandard().getHotel().getPrice()+"元/天\n"
						+"餐饮费:"+userInfo.getUserGroup().getTravelStandard().getMealsCost()+"元/天\n"
						+"其它费用:"+userInfo.getUserGroup().getTravelStandard().getElseCost()+"元/天";
			}else {
				travelstandrad = "您不属于任何用户组，或者\n您所在的用户组不具备出差条件";
			}
			
			return "tisuccess";
		} catch (Exception e) {
			e.printStackTrace();
			return "tierror";
		}
	}

	public List<BusinessTravelList> getBusinessTravelLists() {
		return businessTravelLists;
	}

	public void setBusinessTravelLists(
			List<BusinessTravelList> businessTravelLists) {
		this.businessTravelLists = businessTravelLists;
	}

	public List<BusinessTravel> getBusinessTravels() {
		return businessTravels;
	}

	public void setBusinessTravels(List<BusinessTravel> businessTravels) {
		this.businessTravels = businessTravels;
	}

	public ApplayReim getApplayReim() {
		return applayReim;
	}

	public void setApplayReim(ApplayReim applayReim) {
		this.applayReim = applayReim;
	}

	public List<Receipt> getReceipts() {
		return receipts;
	}

	public void setReceipts(List<Receipt> receipts) {
		this.receipts = receipts;
	}

	
	public String getTravelorder() {
		return travelorder;
	}

	public void setTravelorder(String travelorder) {
		this.travelorder = travelorder;
	}

	public BusinessTravelList getBusinessTravelList() {
		return businessTravelList;
	}

	public void setBusinessTravelList(BusinessTravelList businessTravelList) {
		this.businessTravelList = businessTravelList;
	}

	public ApplayReimService getApplayReimService() {
		return applayReimService;
	}

	public void setApplayReimService(ApplayReimService applayReimService) {
		this.applayReimService = applayReimService;
	}

	public PersonTravelInfoService getPersonTravelInfoService() {
		return personTravelInfoService;
	}

	public void setPersonTravelInfoService(
			PersonTravelInfoService personTravelInfoService) {
		this.personTravelInfoService = personTravelInfoService;
	}

	public List<BusinessTravelList> getNeedChecks() {
		return needChecks;
	}

	public void setNeedChecks(List<BusinessTravelList> needChecks) {
		this.needChecks = needChecks;
	}

	public List<BusinessTravelList> getCheckFails() {
		return checkFails;
	}

	public void setCheckFails(List<BusinessTravelList> checkFails) {
		this.checkFails = checkFails;
	}

	public List<BusinessTravelList> getNeedApplays() {
		return needApplays;
	}

	public void setNeedApplays(List<BusinessTravelList> needApplays) {
		this.needApplays = needApplays;
	}

	public List<BusinessTravelList> getApplayFails() {
		return applayFails;
	}

	public void setApplayFails(List<BusinessTravelList> applayFails) {
		this.applayFails = applayFails;
	}

	public List<BusinessTravelList> getApplaySuccess() {
		return applaySuccess;
	}

	public void setApplaySuccess(List<BusinessTravelList> applaySuccess) {
		this.applaySuccess = applaySuccess;
	}

	public List<BusinessTravelList> getCheckSuccess() {
		return checkSuccess;
	}

	public void setCheckSuccess(List<BusinessTravelList> checkSuccess) {
		this.checkSuccess = checkSuccess;
	}

	public ApplayReimAction getApplayReimAction() {
		return applayReimAction;
	}

	public void setApplayReimAction(ApplayReimAction applayReimAction) {
		this.applayReimAction = applayReimAction;
	}

	public String getUserid() throws Exception{
		try {
			userid = ServletActionContext.getContext().getSession().get("userid").toString();
		} catch (Exception e) {
			throw e;
		}
		
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public String getTravelstandrad() {
		return travelstandrad;
	}

	public void setTravelstandrad(String travelstandrad) {
		this.travelstandrad = travelstandrad;
	}
	
}
